import React, { Suspense } from 'react';
import {
    Switch,
    Route,
    Redirect,
} from "react-router-dom";
function RouterView({ routeList }) {
    return <Suspense fallback={<div>Loading...</div>}>
        <Switch>
            {
                routeList && routeList.length > 0 ? routeList.map((item, index) => {
                    if(item.isRequired&&!localStorage.getItem("userInfo")){
                        return <Redirect key={index} to="/login" path={item.path} />
                    }
                    if (item.to) {
                        return <Redirect to={item.to} from={item.from} key={index} />
                    } else {
                        return <Route
                            key={index}
                            path={item.path}
                            render={routerProps => {
                                return <item.component
                                    {...routerProps}
                                    child={item.children}
                                    navlink={item.children && item.children.filter(item => !item.to)}
                                />
                            }}
                        />
                    }
                }) : null
            }
        </Switch>
    </Suspense>
};
export default RouterView;